package com.filebackup.business.impl;

import java.util.ArrayList;
import java.util.List;

import com.filebackup.business.LocalFolderService;
import com.filebackup.business.ShareService;
import com.filebackup.dao.LocalFolderDao;
import com.filebackup.dao.ShareDao;
import com.filebackup.dao.UserDao;
import com.filebackup.vo.Vo;
import com.filebackup.vo.impl.LocalFolderVo;
import com.filebackup.vo.impl.ShareMessageVo;
import com.filebackup.vo.impl.ShareVo;
import com.filebackup.vo.impl.TableVo;
import com.filebackup.vo.impl.UserVo;

public class ShareServiceImpl implements ShareService {
	
	private ShareDao shareDao;
	private LocalFolderDao localFolderDao;
	private UserDao userDao;

	public void addFolder(Vo vo) {
		// TODO Auto-generated method stub
		this.getShareDao().addVo(vo);
	}

	public void destoryShare(int id) {
		// TODO Auto-generated method stub
		List<Vo> voList=this.getShareDao().findByFolder(id);
		for(int i=0;i<voList.size();i++){
			this.getShareDao().deleteVo(voList.get(i));
		}
	}

	public List<TableVo> getSharedFolder(int id) {
		// TODO Auto-generated method stub
		List<TableVo> voList=new ArrayList<TableVo>();
		List<Vo> ownList=this.getLocalFolderDao().findByOwner(id);
		
		for(int i=0;i<ownList.size();i++){
			LocalFolderVo vo=(LocalFolderVo)ownList.get(i);
			if(this.getShareDao().findByFolder(vo.getId()).size()!=0){
				TableVo mvo=new TableVo();
				mvo.setId(vo.getId());
				mvo.setName(vo.getFolderName());
				mvo.setKind("folder");
				mvo.setUpperId(vo.getUpperId());
				voList.add(mvo);
			}
		}
		List<Vo> shareList=this.getShareDao().findByShareId(id);
		System.out.println(shareList.size());
		for(int i=0;i<shareList.size();i++){
			LocalFolderVo vo=(LocalFolderVo) this.getLocalFolderDao().findByPK(((ShareVo)shareList.get(i)).getFolderId());
			TableVo mvo=new TableVo();
			mvo.setId(vo.getId());
			mvo.setName(vo.getFolderName());
			mvo.setKind("folder");
			mvo.setUpperId(vo.getUpperId());
			voList.add(mvo);
		}

		return voList;
		
	}

	public void leaveShare(Vo vo) {
		// TODO Auto-generated method stub
		this.getShareDao().deleteVo(vo);
	}

	public void setShareDao(ShareDao shareDao) {
		this.shareDao = shareDao;
	}

	public ShareDao getShareDao() {
		return shareDao;
	}

	public void setLocalFolderDao(LocalFolderDao localFolderDao) {
		this.localFolderDao = localFolderDao;
	}

	public LocalFolderDao getLocalFolderDao() {
		return localFolderDao;
	}

	public List<TableVo> getOtherShare(int id) {
		// TODO Auto-generated method stub
		List<TableVo> voList=new ArrayList<TableVo>();
		List<Vo> shareList=this.getShareDao().findByShareId(id);
		System.out.println(shareList.size());
		for(int i=0;i<shareList.size();i++){
			LocalFolderVo vo=(LocalFolderVo) this.getLocalFolderDao().findByPK(((ShareVo)shareList.get(i)).getFolderId());
			TableVo mvo=new TableVo();
			mvo.setId(vo.getId());
			mvo.setName(vo.getFolderName()+"&"+((UserVo)this.getUserDao().findByPK(vo.getOwnerId())).getName());
			mvo.setKind("folder");
			mvo.setUpperId(vo.getUpperId());
			voList.add(mvo);
		}

		return voList;
	}

	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}

	public UserDao getUserDao() {
		return userDao;
	}


}
